%% Training and Testing Single Action Recognition in the Weizmann Dataset
%UsingEuclidean Distance Transform


%The features and the uni_GMM were implemented in c++
%If you want to reproduce the experiments. You may need to re-run the code
%in c++ as well
%*********************** Case 1 *****************************************
%Initially I didn use the L2 normalisation after calculating all features
%Max Performance: 87.77%
%Using mask, obtaining bounding box (bb)
%bb re-Sized [90*72]
%Ncent = 2

%*********************** Case 2 *****************************************
%L2 normalisation after calculating all features
%Max Performance: 90%
%Using mask, obtaining bounding box (bb)
%bb re-Sized [90*72]
%Ncent = 2

%*********************** Notes ******************************************
%The performance was much more worse when I used PCA. 
%Tested for both cases (1 and 2)
%I tried reducing the dimensionality by a factor of 2.
%I also tried reconstructing the total space (same dimensionality)


clear all
close all
clc

%NICTA && Server
addpath('/home/johanna/toolbox/libsvm-3.20/matlab')
addpath('/home/johanna/toolbox/yael/matlab');

%Home
addpath('/media/johanna/HD1T/Toolbox/libsvm-3.20/matlab');
addpath('/media/johanna/HD1T/Toolbox/yael/matlab');


% Si cambias de Ng, correr los sgtes. dos archivos:
Ncent = 2;

% is_pca ==true didn't work
is_pca = false;
if (is_pca)
    display('Using PCA to reduce data');
end

%display('Calculating FV for Training');
%FV_weizmann_training_edt( Ncent, is_pca );

%display('Calculating FV for Testing');
%FV_weizmann_testing_edt( Ncent,is_pca );

Ng = int2str(Ncent);

ACC = [];
all_prediction = [];
all_real = [];

for r=1:9
run = int2str(r);
%fprintf('Running for %s \n', run);
actionNames = importdata('actionNames.txt');

people_train = importdata(strcat('./run', run, '/train_list_run', run, '.dat'));
people_test = importdata(strcat('./run', run, '/test_list_run', run, '.dat'));

n_pe_tr  = length(people_train);
n_pe_te  = length(people_test);
n_actions = length(actionNames);

data_train = [];
labels_train = [];

%% TRAINING
%  Loading Training data
display('TRAINING');
fprintf('Running for %s \n', run);
    for pe = 1: n_pe_tr
        for act = 1:n_actions
                if(is_pca)
                    load_name = strcat('./run', run,  '/FV_training/pca_edt_FV_', people_train(pe),'_',actionNames(act), '_Ng', Ng, '.txt');
                else
                    load_name = strcat('./run', run,  '/FV_training/edt_FV_', people_train(pe),'_',actionNames(act), '_Ng', Ng, '.txt');
                end
            sLoad = char(load_name);
            FV = load(sLoad);
            data_train = [data_train FV];
            labels_train = [labels_train (act)];
        end
    end
data_train = data_train';
labels_train = labels_train';
model = svmtrain(labels_train, data_train, ['-s 0 -t 0 -b 1' ]);
%Testing with training data:
%[predicted_label, accuracy, prob_estimates] = svmpredict(labels_train, data_train, model, ['-b 1']);

%% TESTING. This testing is only for single actions
%  Loading Testing data
display('TESTING');
fprintf('Running for %s \n', run);
data_test = [];
labels_test = [];
for pe = 1: n_pe_te
        for act = 1:n_actions
            if(is_pca)
                load_name = strcat('./run', run,  '/FV_testing/pca_edt_FV_', people_test(pe),'_',actionNames(act), '_Ng', Ng, '.txt');
            else
                load_name = strcat('./run', run,  '/FV_testing/edt_FV_', people_test(pe),'_',actionNames(act), '_Ng', Ng, '.txt');
            end
            sLoad = char(load_name);
            FV = load(sLoad);
            %hist(FV)
            %pause
            data_test = [data_test FV];
            labels_test = [labels_test (act)];
        end
end


data_test = data_test';
labels_test = labels_test';


[predicted_label, accuracy, prob_estimates] = svmpredict(labels_test, data_test, model, ['-b 1']);
all_prediction = [all_prediction; predicted_label ];
all_real = [all_real; labels_test];
ACC = [ACC accuracy(1,1)]
end

